<template>
	<view class="order-submit">

		<!-- 支付bar上面悬浮的引导开通vip悬浮 -->
		<!-- <view class="buy-vip" @click="buyVip" v-if="user.grade === 'normal'">
			<image src="/static/images/common/buyvipbg.png" class="buyvipbg" mode=""></image>
			<view class="desc">
				<view class="text"><text class="openvipnum">9.9</text>开通VIP，本单立省</view>
				<view class="price"><text class="openvipnum">{{ (selectedSku.price - selectedSku.price_vip + Number(selectedSku.self_brokerage)*selectedSkuCount) | brokerageNum }}元</text></view>
			</view>
			<view class="money">
				<view class="price">立即开通</view>
				<image class="arrow" mode="aspectFit" src="https://littleshop.oss-cn-beijing.aliyuncs.com/images/mini/icons/arrow-right-bold.png"></image>
			</view>
		</view> -->
		
		<!-- 旧版支付bar -->
		<!-- <view class="content">
			<view class="money">
				<view class="name">实付款：</view>
				<view class="mark">￥</view>
				<view class="fund">
					{{ orderCurrentTotalAmount ? orderCurrentTotalAmount : orderTotalAmount }}
				</view>
				<view class="free">（免运费）</view>
			</view>
			<view class="button" @click="submitOrder">提交订单</view>
		</view> -->
		
		<view class="buy-bottom">
			<!-- <view>合计：</view>
			<view class="buy-bottom-money">
				<view>
					<text>￥</text>
					<text>{{ orderCurrentTotalAmount ? orderCurrentTotalAmount : orderTotalAmount }}</text>
				</view>
				
			</view> -->
			<view class="buy-now" @click="submitOrder">
				<view>确认支付 ：（￥{{totalAmount}}）</view>
			</view>
		</view>
	</view>
</template>

<script>
	import store from 'store'
	import { mapGetters,mapActions } from 'vuex'
	import { payOrderByWxmini,payOrderByAdapayWxmini,payOrderByAliwap,payOrderByGoldCoin } from "api/paymentApi"
	import { balancepay,wxpaym,spendpay,goldCoinPay } from "helpers/paymentHelper"
	export default {
		name: "OrderSubmitButtonCom",
		store,
		data: () => ({
			savingMoney: 0,
			form: {}
		}),
		props:{
			records: {
			  type: Array,
			  default: null
			},
			needBrokerage:{
				default:'true'
			},
			totalAmount:{
				default:0
			},
			orderId:{
				default:0
			},
			uCouponId:{
				default:0
			}
		},
		computed: {
			...mapGetters([
				'user',
				'vipPriceSavingMoney',
				'orderTotalAmount',
				'orderCurrentTotalAmount',
				'selectedSku',
				'selectedSkuCount',
				'currentAddress',
				'orderPayment',
				'order',
				'product',
				'group_id'
			])
		},
		methods: {
			...mapActions(['createNewOrder','createDrinkOrder']),
			buyVip() {
				uni.navigateTo({
					url: `/packs/pages/user/buyvip?from=product`
				})
			},
			isWeiXinLogin() {
				var ua = window.navigator.userAgent.toLowerCase();
				if (ua.match(/MicroMessenger/i) == 'micromessenger') {
					return true;
				} else {
					return false;
				}
			},
			async payCurrentOrder() {
				
				let id = 0;
				if(this.orderId >0){
					id = this.orderId;
				}else{
					id = this.order.id;
					
				}
				console.log(id);
				if (this.orderPayment === 'balance') {
					await balancepay(id, 'order')
					uni.setStorage({
						key: 'payid',
						data: id
					});
				}
				
				if (this.orderPayment === 'shoppingmoney') {
					await spendpay(id, 'order')
					uni.setStorage({
						key: 'payid',
						data: id
					});
				}
				
				if(this.orderPayment === 'goldcoinsmoney'){
					await goldCoinPay(id, 'order')
					uni.setStorage({
						key: 'payid',
						data: id
					});
				}
				
				if (this.orderPayment === 'weixin') {
					const payload = await payOrderByAdapayWxmini(id)
					let jumpPayUrl = ''
					if(this.isWeiXinLogin){
						jumpPayUrl = payload.result.wx_h5_pay_url;
					}else{
						jumpPayUrl = payload.result.scheme_code;
					}
					window.location.href = jumpPayUrl;
				}
				
				if (this.orderPayment === 'alipay') {
					const payload = await payOrderByAliwap(id)
					document.querySelector('body').innerHTML = payload.result;
					await document.forms[0].submit()
					uni.reLaunch({
						url:'/pages/index/index'
					})
					// uni.setStorage({
					// 	key: 'payid',
					// 	data: this.order.id
					// });
					// uni.navigateTo({
					// 	url:'/packs/pages/order/paid'
					// })
				}
				
				
			},
			async submitOrder() {

				if (!this.currentAddress) {
					uni.showToast({
						title: '请选择收货地址'
					})
					return
				}
				let orderItems = [];
				let isDrink =0;
				let product_id =0;
				this.records.forEach((record, index) => {
					if(record.attr){
						
						isDrink = 1;
						this.form.attr = record.attr;
						product_id = record.product.id;
					}
					if(record.id){
						orderItems.push({
							sku_id: record.sku.id,
							amount: record.number,
							cart_id : record.id,
						})
					}else{
						orderItems.push({
							sku_id: record.sku.id,
							amount: record.number
						})
					}
					
				})
				
				

				this.form.items = orderItems
				this.form.address_id = this.currentAddress.id
				this.form.product_id = product_id;
				if(this.uCouponId){
					this.form.user_coupon_id = this.uCouponId
				}
			
				
				// this.form.group_id = this.group_id

				/* if (this.currentCoupon) {
					this.form.assigned_coupon_id = this.currentCoupon.id
				} */
				if(this.orderId == 0){
					if(isDrink==0){
						
						await this.createNewOrder(this.form);
					}else{
						
						await this.createDrinkOrder(this.form);
					}
					
				}
				
				await this.payCurrentOrder()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.order-submit {
		width: 100%;
		position: fixed;
		left: 0;
		bottom: 0;
		border-top: 1px #EEEEEE solid;
		.buyvipbg{
			display: block;
			width: 100%;
			height: 68rpx;
			position: absolute;
			left:0;
			top:0;
			z-index: -1;
		}
		.buy-vip {
			width: 750rpx;
			height: 68rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			background-color: #FFE066;
			background: url(../../../static/images/buyviptips.png);
			background-size: cover;
			position: fixed;
			left: 0;
			bottom:158rpx;
		
			.desc {
				display: flex;
				font-size: 24rpx;
				align-items: center;
		
				.icon {
					width: 44rpx;
					height: 44rpx;
					margin-right: 20rpx;
				}
		
				.text {
					display: flex;
					align-items: center;
					font-size: 24rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #AC7424;
					.openvipnum{
						font-size: 24rpx;
						font-family: PingFangSC-Semibold, PingFang SC;
						font-weight: 600;
						color: #FE0000;
						margin-left: 16rpx;
					}
				}
		
				.price {
					// height: 32rpx;
					// color: white;
					// background-color: #EF6767;
					// border-radius: 16rpx;
					font-size: 24rpx;
					font-family: PingFangSC-Medium, PingFang SC;
					font-weight: 500;
					color: #FE0000;
					padding: 0 5rpx;
					// margin: 0 10rpx;
				}
		
				.unit {
					font-size: 24rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #AC7424;
				}
			}
		
			.money {
				display: flex;
				align-items: center;
				font-size: 24rpx;
				height:68rpx;
				color: #333333;
				.price {}
				.arrow {
					width: 24rpx;
					height: 24rpx;
					margin-right: 16rpx;
				}
			}
		}
		// .buy-vip {
		// 	display: flex;
		// 	justify-content: space-between;
		// 	// background-color: #FFE066;
		// 	height: 72rpx;
		// 	line-height: 72rpx;
		// 	padding: 0 20rpx;
		// 	position: relative;
		// 	background: url(../../../static/images/buyviptips.png);

		// 	.desc {
		// 		display: flex;
		// 		font-size: 24rpx;
		// 		align-items: center;

		// 		.icon {
		// 			width: 44rpx;
		// 			height: 44rpx;
		// 			margin-right: 20rpx;
		// 		}

		// 		.text {}

		// 		.price {
		// 			height: 32rpx;
		// 			line-height: 32rpx;
		// 			color: white;
		// 			background-color: #EF6767;
		// 			border-radius: 16rpx;
		// 			padding: 0 10rpx;
		// 			margin: 0 10rpx;
		// 		}

		// 		.unit {}
		// 	}

		// 	.money {
		// 		display: flex;
		// 		font-size: 24rpx;

		// 		.price {}

		// 		.arrow {
		// 			width: 24rpx;
		// 			height: 24rpx;
		// 			margin-top: 22rpx;
		// 		}
		// 	}
		// }

		.content {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 100rpx;
			padding: 0 20rpx;
			background: #FFFFFF;
			padding-bottom: 50rpx;

			.money {
				display: flex;

				.name {
					font-size: 24rpx;
					color: #333333;
					margin-top: 20rpx;
				}

				.mark {
					font-size: 24rpx;
					font-weight: 500;
					color: #333333;
					margin-top: 20rpx;
				}

				.fund {
					font-size: 48rpx;
					font-weight: 500;
					color: #333333;
				}

				.free {
					font-size: 20rpx;
					color: #ff5900;
					margin-top: 24rpx;
				}
			}

			.button {
				width: 224rpx;
				height: 60rpx;
				line-height: 60rpx;
				border-radius: 40rpx;
				background-color: #FEBF05;
				text-align: center;
				font-size: 24rpx;
				color: white;
			}
		}
	}

	.buy-bottom {
		width: 100%;
		height: 100rpx;
		background: #FFFFFF;
		display: flex;
		padding-bottom: 44rpx;
		padding-top: 30rpx;
		justify-content:flex-end;
		align-items: center;
		
	}

	.buy-bottom-money {
		display: flex;
		flex-direction: column;
		align-items: flex-end;
		margin: 4rpx 0 0 0;

		>view:nth-child(1) {
			display: flex;
			align-items: baseline;

			>text:nth-child(1) {
				font-size: 28rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #FF4924;
			}

			>text:nth-child(2) {
				font-size: 48rpx;
				font-family: PingFangSC-Semibold, PingFang SC;
				font-weight: 600;
				color: #FF4924;
				line-height: 66rpx;
			}
		}

		>view:nth-child(2) {
			font-size: 24rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #999999;
		}
	}

	.buy-now {
		width: 518rpx;
		height: 72rpx;
		border-radius: 4rpx;
		position: relative;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		font-family: PingFangSC-Semibold, PingFang SC;
		font-weight: 600;
		background:#D84C29;
		color:#fff;
		margin:0 auto;


		>view {
			position: relative;
			z-index: 1;
		}
	}
</style>
